home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cc04.arc / TIMER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-03-15  |  4.3 KB  |  148 lines

  1. /*****************************************************************************
  2.  
  3.              Program: timer.c
  4.  
  5.              Function: an ms-dos equivalent to the unix time command,
  6.                        measures length of time required to run a program
  7.                        or execute a command.
  8.  
  9.              Syntax: timer -c <dos internal command>
  10.                      timer -c <batch job>
  11.                      timer <filename.ext>
  12.  
  13.              Language: written in DeSmet C ver 2.41
  14.  
  15. *****************************************************************************/
  16.  
  17. #include "stdio.h"
  18.  
  19. extern unsigned _rax, _rbx, _rcx, _rdx;
  20.  
  21. main(argc,argv)
  22. int argc;
  23. char *argv[];
  24. {
  25.    int i,start[8],stop[8],error=0;
  26.    int hours,minutes,seconds,hundreths;
  27.    int cmdflag;
  28.    char str[20],startstring[60],stopstring[60],command[20],commandtail[80];
  29.  
  30.    if(strcmp(argv[1],"-c")==0 || strcmp(argv[1],"/c")==0)
  31.       cmdflag=1;
  32.    else
  33.       cmdflag=0;
  34.  
  35.    if(argc<2+cmdflag) {
  36.       printf("\ntimer measures the amount of time required for a command to execute.\n\n");
  37.       printf("syntax: timer <filename.ext> or\n");
  38.       printf("        timer -c <dos internal command> or\n");
  39.       printf("        timer -c <batch job>\n\n");
  40.       printf("where filename is the name of an executable program,\n");
  41.       printf("dos internal command is an INTERNAL DOS command (ie. dir), and\n");
  42.       printf("batch job is the name of a batch file (ie. autoexec).\n\n");
  43.       printf("timer will not work properly for times greater than 24 hours.\n");
  44.       exit(1);
  45.    }
  46.  
  47.    if(cmdflag) {
  48.       strcpy(command,"COMMAND.COM");
  49.       strcpy(commandtail, "/C");
  50.       }
  51.    else {
  52.       strcpy(command,argv[1]);
  53.       strcpy(commandtail, "" );
  54.    }
  55.  
  56.    for(i=2;i<argc;i++) {
  57.       strcat(commandtail,argv[i]);
  58.       strcat(commandtail," ");
  59.    }
  60.  
  61.    printf("Executing %s %s\n", command, commandtail );
  62.  
  63.    datetime(start);                        /* get start time info */
  64.    error=exec(command, commandtail);       /* execute */
  65.    datetime(stop);                         /* get stop time info   */
  66.  
  67.    if(error==-1){
  68.       printf("\nerror: file not found or illegal command.\n");
  69.       printf("       use the -c flag to time an internal dos command or a batch file.\n");
  70.       printf("       'COMMAND.COM' must be in the default drive when using -c flag.\n\n");
  71.       exit(1);
  72.      }
  73.    else
  74.       if(error!=0)
  75.          printf("\nreturned ERRORLEVEL code = %d\n",error);
  76.  
  77.    strcpy(startstring,"\n  Start: ");
  78.  
  79.       sprintf(str,"%2d-%2d-%4d", start[6], start[7], start[5]);
  80.       fillzero(str);
  81.       strcat( strcat(startstring,str), " ");
  82.  
  83.       sprintf(str,"%2d:%2d:%2d.%2d", start[1], start[2], start[3], start[4]);
  84.       fillzero(str);
  85.       strcat( strcat(startstring,str), "\n");
  86.  
  87.    strcpy(stopstring,"  Stop:  ");
  88.  
  89.       sprintf(str,"%2d-%2d-%4d", stop[6], stop[7], stop[5]);
  90.       fillzero(str);
  91.       strcat( strcat(stopstring,str), " ");
  92.  
  93.       sprintf(str,"%2d:%2d:%2d.%2d", stop[1], stop[2], stop[3], stop[4]);
  94.       fillzero(str);
  95.       strcat( strcat(stopstring,str), "\n");
  96.  
  97.    printf("\n[%s %s]\n", command, commandtail );
  98.    puts(startstring);
  99.    puts(stopstring);
  100.  
  101.    hours    =stop[1]-start[1];
  102.    minutes  =stop[2]-start[2];
  103.    seconds  =stop[3]-start[3];
  104.    hundreths=stop[4]-start[4];
  105.  
  106.    if(hundreths<0){
  107.       hundreths+=100;
  108.       seconds--;
  109.    }
  110.    if(seconds<0)  {
  111.       seconds+=60;
  112.       minutes--;
  113.    }
  114.    if(minutes<0)  {
  115.       minutes+=60;
  116.       hours--;
  117.    }
  118.    if(hours<0)
  119.       hours+=24;
  120.  
  121.    printf("\nElapsed time: %d hours, %d minutes, %d.%d seconds.\n",hours,minutes,seconds,hundreths);
  122.  
  123. }
  124.  
  125. int datetime(ar) /* was declared void */
  126. int *ar[];
  127. {
  128.    _rax=(44 << 8);   _doint( 0x21 );                 /* get time */
  129.    *++ar= _rcx >> 8;                 /* hour  */
  130.    *++ar= _rcx & 0x00ff;             /* minute*/
  131.    *++ar= _rdx >> 8;                 /* second*/
  132.    *++ar= _rdx & 0x00ff;             /* 1/100 */
  133.  
  134.    _rax=(42 << 8);   _doint( 0x21 );                 /* get date */
  135.    *++ar= _rcx;                      /* year  */
  136.    *++ar= _rdx >> 8;                 /* month */
  137.    *++ar= _rdx & 0x00ff;             /* day   */
  138. }
  139.  
  140. int fillzero(st) /* was declared void */
  141. char *st;
  142. {
  143.    char *tc;
  144.  
  145.    for(tc=st;*tc!='\0';tc++)
  146.        *tc= *tc==' ' ? '0' : *tc;
  147. }
  148.